<template>
  <div class="app-container">
    <el-form
      ref="ruleForm"
      :inline="true"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-card>
        <el-form-item label="过期时间" prop="expiryDate">
          <el-date-picker
            v-model="ruleForm.expiryDate"
            type="datetime"
            placeholder="选择日期时间"
            align="right"
            :picker-options="pickerOptions"
          />

        </el-form-item>
      </el-card>
      <el-card style="margin-top: 50px;">
        <div
          slot="header"
          class="clearfix"
        >
          <span>内容</span>
        </div>
        <el-form-item prop="content">
          <div ref="editor" class="editor" />
        </el-form-item>
      </el-card>
      <el-form-item style="margin-top: 10px">
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addAdvertisement } from '@/api/advertisement'
import { upload } from '@/utils/upload'
import E from 'wangeditor'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

export default {
  name: 'AddAdver',
  data() {
    return {
      ruleForm: {
        content: '',
        expiryDate: ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      fileList: [],
      headers: {
        'Authorization': getToken()
      },
      rules: {
        content: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
        // expiryDate: [
        //   { required: true, message: '请选择', trigger: 'change' }
        // ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi'
    ])
  },
  created() {

  },

  mounted() {
    const _this = this
    var editor = new E(this.$refs.editor)
    // 自定义菜单配置
    editor.config.zIndex = 10
    // 文件上传
    editor.config.customUploadImg = function(files, insert) {
      // files 是 input 中选中的文件列表
      // insert 是获取图片 url 后，插入到编辑器的方法
      files.forEach(image => {
        upload(_this.imagesUploadApi, image).then(res => {
          const data = res.data
          const url = _this.baseApi + '/file/' + data.type + '/' + data.realName
          insert(url)
        })
      })
    }
    editor.config.onchange = (html) => {
      this.ruleForm.content = html
    }
    editor.create()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          await addAdvertisement(this.ruleForm)
          this.$message.success('成功啦！')
          this.$router.go(-1)
        } else {
          // this.$message.warning('出错啦！')
          return false
        }
      })
    },
    cancel() {
      this.$refs.ruleForm.resetFields()
      this.$router.go(-1)
    }
  }
}
</script>

    <style rel="stylesheet/scss" lang="scss" scoped>
    .app-container {
      height: calc(100vh - 50px);
      overflow: auto;
    }
    ::v-deep(.el-button--text) {
      border: 0;
      padding: 0;
      width: 60px;
      background-color: #fff;
      color: #1890ff;
    }
    .edit {
      z-index: 9999;
      position: relative;
    }
    </style>
